package com.nkzy.controller;

import com.nkzy.Pojo.article;
import com.nkzy.mapper.ariclemapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class articleController {

    @Autowired
    private ariclemapper aricleMapper;

    // 定义通用返回结果类
    public static class Response<T> {
        private int code;
        private String message;
        private T data;

        public Response(int code, String message, T data) {
            this.code = code;
            this.message = message;
            this.data = data;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }

    // 插入文章
    @PostMapping("/insert")
    public Response<String>
    insertArticle(@RequestBody article article) {
        int result = aricleMapper.insertArticle(article);
        if (result > 0) {
            return new Response<>(200, "插入成功", null);
        } else {
            return new Response<>(500, "插入失败", null);
        }
    }

    // 根据ID查询文章
    @GetMapping("/select/{id}")
    public Response<article> selectArticleById(@PathVariable Integer id) {
        article article = aricleMapper.selectArticleById(id);
        if (article != null) {
            return new Response<>(200, "查询成功", article);
        } else {
            return new Response<>(404, "未找到文章", null);
        }
    }

    // 更新文章信息
    @PutMapping("/update")
    public Response<String> updateArticle(@RequestBody article article) {
        int result = aricleMapper.updateArticle(article);
        if (result > 0) {
            return new Response<>(200, "更新成功", null);
        } else {
            return new Response<>(500, "更新失败", null);
        }
    }

    // 根据ID删除文章
    @DeleteMapping("/delete/{id}")
    public Response<String> deleteArticleById(@PathVariable Integer id) {
        int result = aricleMapper.deleteArticleById(id);
        if (result > 0) {
            return new Response<>(200, "删除成功", null);
        } else {
            return new Response<>(500, "删除失败", null);
        }
    }
}